package com.foreverwin.jzymes.qc.sample.service.impl;

import com.foreverwin.jzymes.common.service.ICustomerService;
import  com.foreverwin.jzymes.common.service.IItemAdviceService;
import com.foreverwin.jzymes.common.service.IItemGroupAdviceService;
import com.foreverwin.jzymes.common.service.ISiteAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.cutter.enums.RecordStatusEnum;
import com.foreverwin.jzymes.qc.sample.domain.SampleData;
import com.foreverwin.jzymes.qc.sample.enums.SampleStatusEnum;
import com.foreverwin.jzymes.qc.sample.mapper.SampleAdiviceMapper;
import com.foreverwin.jzymes.qc.sample.service.ISampleDataAdiviceService;
import com.foreverwin.jzymes.qc.sample.service.ISampleDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SampleDataAdiviceServiceImpl implements ISampleDataAdiviceService {
    @Autowired
    ISampleDataService sampleDataService;
    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    SampleAdiviceMapper sampleAdiviceMapper;
    @Autowired
    ICustomerService customerService;

    /**
     * 样品签样
     * @param sampleData
     */
    @Override
    public void insertSample(SampleData sampleData)  {
      /**校验字段*/
      checkSampledata( sampleData );

      String handle = BOHandleUtil.buildSampleHandle( sampleData.getSite() ,sampleData.getSampleNo()  );
      String machineType = itemGroupAdviceService.findItemGroupBySite( sampleData.getSite() );

      SampleData sampleData1 = sampleDataService.selectSampleDataById(  handle );

      if ( sampleData1 == null ){
          /**样品第一次签样时状态为ON*/
          if ( sampleData.getSampleStu().equals( SampleStatusEnum.OFF.name() )  ){
              throw BusinessException.build("qc.sample.sampleStuNotOFF",new String[] {sampleData.getSampleNo()} );
          }
          /**补充字段*/
          sampleData.setMachineType( machineType );
          sampleData.setHandle( handle );
          sampleData.setCreatedDateTime( new Date() );
          sampleData.setCreateUserBo(CommonMethods.getUserBo());
         // sampleData.setRegisterUserBo( sampleData.getCreateUserBo() );
          sampleData.setRegisterDateTime( sampleData.getCreatedDateTime() );
          sampleData.setStatus( RecordStatusEnum.Y.name() );

          sampleDataService.insertSampleData( sampleData );
      }else {
         throw BusinessException.build("qc.sample.sampleIsExist",new String[]{sampleData.getSampleNo()});
      }

    }

    /**
     * 检验前台传入样品数据
     * @param sampleData
     */
    public void checkSampledata(SampleData sampleData){

        siteAdviceService.existSite( sampleData.getSite() );
        /**校验物料*/
        itemAdviceService.existItem( sampleData.getItemBo() );
        /**样品编号不能为空*/
        Assert.notNull( sampleData.getSampleNo() , I18nUtil.getI18nText("qc.sample.sampleNotBlank"));
        /**样品处置不能为空*/
        Assert.notNull(sampleData.getDisposition(),I18nUtil.getI18nText("qc.sample.sampleDisposition"));
        /**状态不能为空*/
        Assert.notNull(sampleData.getSampleStu(),I18nUtil.getI18nText("qc.sample.sampleStatus"));

        Assert.notNull(sampleData.getRegisterUserBo(),I18nUtil.getI18nText("qc.sample.sampleRegisterUserBo"));

    }

    /**
     * 编辑样品信息
     * @param  handle
     * @param  sampleStu
     * @param  disposition
     * @param quitUserBo
     * @param register
     * @return
     */
    public SampleData editSample(String handle , String sampleStu,String disposition ,String quitUserBo, String register ) {
        String userBo = CommonMethods.getUserBo();

        SampleData sampleData = sampleDataService.selectSampleDataById( handle );
        /**样品信息不能为空*/
        Assert.notNull( sampleData , I18nUtil.getI18nText("qc.sample.sampleNotExist"));
        /**样品处置不能为空*/
        Assert.hasText( disposition ,I18nUtil.getI18nText("qc.sample.sampleDisposition"));
        /**状态不能为空*/
        Assert.hasText( sampleStu ,I18nUtil.getI18nText("qc.sample.sampleStatus"));
        //Assert.hasText( register ,I18nUtil.getI18nText("qc.sample.sampleRegisterUserBo"));

        sampleData.setUpdateUserBo( userBo );
        sampleData.setUpdatedDateTime( new Date() );

        if ( sampleData.getSampleStu().equals( SampleStatusEnum.OFF.name() ) ){
           throw BusinessException.build( "qc.sample.sampleStuNotOFF",new String[] {sampleData.getSampleNo()});
        }

        //sampleData.setRegisterUserBo( register );
        sampleData.setRegisterDateTime( sampleData.getUpdatedDateTime() );
        sampleData.setDisposition( disposition );
        sampleData.setSampleStu( sampleStu );
        if ( sampleStu.equals( SampleStatusEnum.OFF.name() ) ){
            Assert.isTrue(!StringUtils.isEmpty( quitUserBo ),I18nUtil.getI18nText("qc.sample.sampleQuitUserBo"));
            sampleData.setQuitUserBo( quitUserBo );
            sampleData.setQuitDateTime( sampleData.getUpdatedDateTime() );
        }

        sampleDataService.updateSampleData( sampleData );

        return  sampleData;
    }

    /**
     * 查找样品信息
     * @param jsonMap
     * @return
     */
    @Override
    public List<SampleData> selectSample(Map<String,Object> jsonMap) {
         String site = (String)jsonMap.get("site");
         String itemBo = (String)jsonMap.get("itemBo");
         String sampleNo = (String)jsonMap.get("sampleNo");

        List<SampleData> list = sampleAdiviceMapper.searchSampleData( site , itemBo ,sampleNo );
        return list;
    }

    /**
     * 删除样品信息
     * @param handles
     */
    @Override
    public void deleteSampleData(List<String> handles) {
          String site = CommonMethods.getSite();
          /**检查站点*/
          siteAdviceService.existSite( site );
          /**检查集合*/
          Assert.isTrue( handles != null && handles.size() > 0, I18nUtil.getI18nText( "qc.sample.sampleNotBlank" ) );
          Date deleteDate = new Date();
          String deleteBo = CommonMethods.getUserBo();

        for ( int i = 0;i<handles.size();i++ ){
            String handle = handles.get(i);
            SampleData sampleData = sampleDataService.selectSampleDataById( handle );
            Assert.isTrue( sampleData != null , I18nUtil.getI18nText("qc.sample.sampleNotExist"));

            sampleData.setStatus( RecordStatusEnum.N.name() );
            sampleData.setUpdatedDateTime( deleteDate );
            sampleData.setUpdateUserBo( deleteBo );
            sampleDataService.updateSampleData( sampleData );

          }
    }
}